
var utils = {
    AJAX_SRC: 'http://feiyutiyu.cn/Api/',
    FILL: 'http://file.jiaoxin-tech.cn/',
    Ua: window.navigator.userAgent.toLowerCase()
};

/*
* 判断是否再微信中
* */
utils.isWeiXin = ()=>{
    return utils.Ua.match(/MicroMessenger/i) == 'micromessenger' ? true : false;
};

/*
* 判断是否在App中
* */
utils.isFeiYu = ()=>{
    return utils.Ua.match(/feiyu/i) == 'feiyu' ? true : false;
};

/*
 * 判断字符串是否以指定字符串开头
 */
String.prototype.startWith = function(compareStr){
    return this.indexOf(compareStr) == 0;
};
/*
 * 判断字符串是否以指定字符串结尾
 */
String.prototype.endWith = function(compareStr){
    return this.length < compareStr.length ? false : this.lastIndexOf(compareStr) == this.length - compareStr.length;
};

/*
 * 获取指定name的SessionStorage值
 * name: 键
 */
utils.getSessionStorage = function(name){
    var obj = window.sessionStorage.getItem(name);
    return obj && (obj.startWith('{') || obj.startWith('[')) ? eval('(' + obj + ')') : obj;
};

/*
 * 设置SessionStorage值
 * name: 键
 * value: 值
 */
utils.setSessionStorage = function(name, value){
    value = typeof value != 'object' ? value : JSON.stringify(value);
    window.sessionStorage.setItem(name, value);
};

/*
 * 删除SessionStorage值
 * name: 键,不传则清楚所有
 */
utils.deleteSessionStorage = function(name){
    if(name) window.sessionStorage.removeItem(name);
    else {
        var storage = window.sessionStorage;
        for(var i in storage){
            window.sessionStorage.removeItem(i);
        }
    }
};

/*
 * 获取指定name的localStorage值
 * name: 键
 */
utils.getLocalStorage = function(name){
    var obj = window.localStorage.getItem(name);
    return obj && (obj.startWith('{') || obj.startWith('[')) ? eval('(' + obj + ')') : obj;
};

/*
 * 设置localStorage值
 * name: 键
 * value: 值
 */

utils.setLocalStorage = function(name, value){
    value = typeof value != 'object' ? value : JSON.stringify(value);
    window.localStorage.setItem(name, value);
};

/*
 * 删除Cookie值
 * name: 键,不传则清楚所有
 */
utils.deleteLocalStorage = function(name){
    if(name) window.localStorage.removeItem(name);
    else window.localStorage.clear();
};

/*
* 公用加载中
* */
utils.loading = (flag=true)=>{
    var loading = document.getElementById('loading');
    if(!flag){
        if(!loading) return;
        loading.parentNode.removeChild(loading);
        return;
    }
    var app = document.getElementById('page');
    if(loading) return;
    var loadingEle = document.createElement('div');
    loadingEle.id = 'loading';
    loadingEle.className = 'loadingWrap';
    loadingEle.innerHTML = '<div class="loadingInner"></div>';
    app.appendChild(loadingEle);
};

/*
* 展示提示信息
* tips: 提示信息
* */
utils.showTips = (tips='错误提示')=>{
    if(document.getElementById('SucTips')) return;
    var div = document.createElement('div');
    div.id = 'SucTips';
    div.innerHTML = '<span>' + tips + '</span>';
    document.getElementById('page').appendChild(div);
    var timer = setTimeout(()=>{
        document.getElementById('SucTips').parentNode.removeChild(document.getElementById('SucTips'));
        clearTimeout(timer);
    },2000);
};
/*
* 公用弹框
* that: 当前组件
* obj:{
*  title: '标题',
*  content: '内容',
*  ensure: fn,
*  cancel: fn 可选
* }
* */
utils.modal = (that , obj)=>{
    var app = document.getElementById('page') ,
        modal = document.getElementById('modal');
    if(modal) modal.parentNode.removeChild(modal);
    var modalEle = document.createElement('div');
    modalEle.id = 'modal';
    modalEle.className = 'modalWrap flex-container';
    modalEle.innerHTML = '<div class="modal-inner">' +
        '<h3>' + ( obj.title ? obj.title : '' ) + '</h3>' +
        '<div class="modal-cot">' + obj.content + '</div>' +
        '<div class="modal-btns flex-container">' +
        '<a href="javascript:;" id="modalSure">确定</a>' +
        (obj.cancel ? '<a href="javascript:;" id="modalCancel">取消</a>' : '') +
        '</div>' +
        '</div>';
    app.appendChild(modalEle);
    document.getElementById('modalSure').onclick = function(){
        document.getElementById('modal').parentNode.removeChild(document.getElementById('modal'));
        obj.ensure.call(that);
    };
    if(!document.getElementById('modalCancel')) return;
    document.getElementById('modalCancel').onclick = function(){
        document.getElementById('modal').parentNode.removeChild(document.getElementById('modal'));
        obj.cancel.call(that);
    }
};

/*
* 表单基本验证码
* verifyType: 验证类型
* val: 需要验证的值
* */
utils.validate = (verifyType , val , tips)=>{
    var msg , isCorrect;
    switch(verifyType){
        case 'noNull' :
            isCorrect = val;
            msg = tips + '不能为空！';
            break;
        case 'phone' :
            isCorrect = /^1\d{10}$/.test(val); //手机号
            msg = '手机号格式错误！';
            break;
        case 'code':
            isCorrect = /^\d{6}$/.test(val); //手机验证码
            msg = '验证码格式错误';
            break;
        default:
            isCorrect = true;
    }
    if(!isCorrect) utils.showTips(msg);
    return isCorrect;
};

/*
* 发送验证码
* that：当前vue实例
* */
utils.sendCode = (that)=>{
    if(!that.isSendCode) return;
    if(!utils.validate('phone' , that.phone)) return;
    utils.ajax(utils.AJAX_SRC + 'Register/sendCode', { phone: that.phone }).then(result=>{
        if(result.errcode < 0){
            utils.showTips(result.errmsg);
            return;
        }
        that.codeId = result.data.codeId;
        that.timeTips = '60s后重新获取';
        utils.showTips('验证码已发送');
        var counts = 60;
        var timer = setInterval(()=>{
            counts--;
            that.timeTips = counts + 's后重新获取';
            if(counts <= 0 ){
                that.timeTips = '重新获取验证码';
                clearInterval(timer);
            }
        },1000);
    })
};

/*
* 获取参数集合
* */
utils.getParams = function() {
    var search = window.location.search;
    if (!search)
        return {};
    var object = {};
    var params = search.substring(1).split("&");
    for (var index in params) {
        if(typeof params[index]=="string"){
            var param = params[index].split("=");
            object[param[0]] = param[1].split('?')[0];
        }
    }
    return object;
};
/*
* 获取单个参数
* */
utils.getParam = function (name) {
    var url = window.location.search;
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var result = url.substr(1).match(reg);
    return result ? decodeURIComponent(result[2]) : null;
};